package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MemberDao;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Icon;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findByUsername(String username) {
        List<Menu> results = new ArrayList<>();

        User user = userDao.findByUsername(username);
        Integer uid = user.getId();
        Set<Role> roles = roleDao.findByUid(uid);
        for (Role role : roles) {
            List<Menu> menues= menuDao.findByRid(role.getId());
            for (Menu menu : menues) {
                if (menu.getParentMenuId() == null){
                    List<Menu> children = menuDao.findChildren(menu.getId());
                    menu.setChildren(children);
                    results.add(menu);
                }
            }
        }
        return results;
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageInfo pageQuery(QueryPageBean queryPageBean) {
        List<Menu> list = menuDao.queryString(queryPageBean.getQueryString());
        List<Menu> result = new ArrayList<>();
        for (Menu menu : list) {
            if (menu.getParentMenuId() == null){
                menu.setChildren(menuDao.findChildren(menu.getId()));
                result.add(menu);
            }
        }
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        return new PageInfo(result);
    }

    @Override
    public List findLevel1Menu() {
        List<Menu> parents = menuDao.findLevel1Menu();
        Menu menu = new Menu();
        menu.setId(-1);
        menu.setName("一级菜单");
        parents.add(menu);
        return parents;
    }

    @Override
    public List<Menu> findLevel1MenuWithoutSelf(Integer id) {
        List<Menu> parents = menuDao.findLevel1Menu();
        Menu menu = new Menu();
        menu.setId(-1);
        menu.setName("一级菜单");
        parents.add(menu);

        for (Menu parent : parents) {
            Integer parentId = parent.getId();
            if (parentId == id){
                parents.remove(parent);
                break;
            }
        }
        return parents;
    }

    @Override
    public List<Menu> findChildren(Integer id) {
        return menuDao.findChildren(id);
    }

    @Override
    public void DeleteById(Integer id) {
        List<Menu> children = menuDao.findChildren(id);
        if (children.size() != 0){
            throw new RuntimeException("含有子级菜单不能被删除");
        }
        menuDao.deleteRoleAndMenuByMenuId(id);
        menuDao.deleteById(id);
    }

    @Override
    public List<Icon> findIcons() {
        return menuDao.findIcons();
    }

    @Override
    public void add(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId == -1){
            menu.setLevel(1);
            menu.setParentMenuId(null);
        }else {
            menu.setLevel(2);
        }
        menuDao.add(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void update(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId == -1){
            menu.setLevel(1);
            menu.setParentMenuId(null);
        }else {
            menu.setLevel(2);
        }

        menuDao.update(menu);
    }

}
